<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div v-if="meta.indexOf('TempAdd') > -1" class="mb20">
      <el-button type="primary" @click="handleEdit()">新增</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        label="模板名称"
        prop="name"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="小程序模式"
        prop="modeType"
        show-overflow-tooltip
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.modeType == 1">商户模式</span>
          <span v-else>场馆模式</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="排序码"
        prop="sortNum"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="createTime"
        min-width="160"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建人"
        prop="creatorName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="状态"
        min-width="80"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="e => statusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="120"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('TempEdit') > -1"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              v-if="meta.indexOf('TemlSave1') > -1"
              v-preventReClick
              type="text"
              @click="handleTemp(scope.row)"
            >装修</el-button>
            <el-button
              v-if="meta.indexOf('TempDel') > -1"
              v-preventReClick
              type="text"
              class="red"
              @click="handleDel(scope.row)"
            >删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <el-dialog v-if="isShowEdit" :title="param.id ? '编辑模板' : '新增模板'" :visible.sync="isShowEdit" width="600px">
      <el-form ref="form" :rules="rules" :model="param" label-width="100px">
        <el-form-item label="小程序模式" prop="modeType">
          <el-radio-group v-model="param.modeType">
            <el-radio label="0">场馆模式</el-radio>
            <el-radio label="1">商户模式</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="param.name" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="排序码">
          <el-input
            v-model="param.sortNum"
            onkeyup="value=value.replace(/[^\d^\.]/g,'')"
            onkeypress="return event.keyCode>=48&&event.keyCode<=57"
            placeholder="请输入排序码"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowEdit = false">取 消</el-button>
        <el-button v-preventReClick :loading="subLoading" type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <Temp v-if="isShowTemp" ref="EditRef" @close="isShowTemp = false" @success="getList" />
  </div>
</template>

<script>
import { miniTemplateList, miniTemplateAdd, miniTemplateEdit, miniTemplateDel } from '@/api'
import { queryFormConfig, propList, rules } from './config'
import Temp from './temp.vue'
export default {
  components: {
    Temp
  },
  data() {
    return {
      propList,
      queryFormConfig,
      rules,
      listLoading: false,
      list: [],
      meta: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},

      isShowTemp: false,
      isShowEdit: false,
      param: {},
      subLoading: false
    }
  },
  created() {
    this.getList()
    this.meta = this.$route.meta.buttons || []
    // this.getVenueList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleEdit(row) {
      this.isShowEdit = true
      this.param = {}
      if (row && row.id) {
        this.param = { ...row, type: row.type || '0' }
      } else {
        this.param = { type: '0' }
      }
    },
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { param } = this
          this.subLoading = true
          const fn = param.id ? miniTemplateEdit : miniTemplateAdd
          fn({ param }).then(res => {
            if (res.errorCode !== '000000') return
            this.subLoading = false
            this.$message.success('保存成功')
            this.isShowEdit = false
            this.getList()
          }, () => {
            this.subLoading = false
          })
        }
      })
    },
    statusChange(row) {
      miniTemplateEdit({ param: { ...row }}).then(res => {
        if (res.errorCode !== '000000') return
        this.$message.success('更改状态成功')
        this.getList()
      }, () => {
        this.getList()
      })
    },
    handleTemp(row) {
      this.$router.push({
        name: 'MiniTemplateEdit',
        query: { id: row.id }
      })
    },
    handleDel(row) {
      this.$confirm('此操作将删除该模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        miniTemplateDel({ param: { id: row.id }}).then(res => {
          if (res.errorCode !== '000000') return
          this.$message.success('保存成功')
          this.getList()
        }, () => {
          this.getList()
        })
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      this.listLoading = true
      miniTemplateList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
